package com.xinchuang.util;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2022/10/23 16:03
 */
public class DistanceUtil {

    public static final double earthRadius = 6371.393D;
    public static final double nmi = 5.4E-4D;

    public DistanceUtil() {
    }

    public static double meterToNmi(double len) {
        return len * 5.4E-4D;
    }

    public static double meterToDegree(double len) {
        return km2Degree(len / 1000.0D);
    }

    private static double rad(double d) {
        return d * 3.141592653589793D / 180.0D;
    }

    public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2.0D * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2.0D), 2.0D) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2.0D), 2.0D)));
        s *= 6371.393D;
        s = (double)Math.round(s * 10000.0D) / 10000.0D;
        s *= 1000.0D;
        return s;
    }

    public static Double km2Degree(Double l) {
        Double degree = 0.008992661340005603D * l;
        return degree;
    }

    public static Double degree2Km(Double degree) {
        Double l = 111.20178578851908D * degree;
        return l;
    }

}
